package com.antkit.mark.ak;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import com.antkit.mark.ak.common.BaseRecycleAdapter;
import com.antkit.mark.ak.kit.Kits;
import com.antkit.mark.ak.wedigt.SanDTestView;
import com.antkit.mark.ak.wedigt.ZuanShiView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Mark
 * @Date on 2019/3/5
 **/
public class TestActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    BaseRecycleAdapter<Integer> baseRecycleAdapter;
    List<Integer> list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_activity);
        AppCompatImageView imageView = findViewById(R.id.img);
        imageView.setImageResource(R.mipmap.aaa);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);


         recyclerView = findViewById(R.id.recyclerView);
       baseRecycleAdapter = new BaseRecycleAdapter<Integer>(this,
                null,R.layout.item,false) {
            @Override
            protected void setPositionClick(int position, Integer bean) {

            }

            @Override
            protected void initData(MyViewHolder holder, int position, Integer bean) {

                ((PixView)(holder.getView(R.id.item))).setPixColor(bean);

            }
        };



       new MyTask().execute();

    }
    private class  MyTask extends AsyncTask<String,String,Bitmap>{

        @Override
        protected Bitmap doInBackground(String... strings) {
            Bitmap o = drawableToBitmap(getResources().getDrawable(R.mipmap.ccc));
            Bitmap n = scaleBitmap(o,0.5f);
            w = n.getWidth();
           // mHandler.sendEmptyMessage(0);

            for (int j = 0; j <n.getHeight() ; j++) {

                for (int i = 0; i <n.getWidth() ; i++) {

                    list.add(n.getPixel(i,j));
                }
          // mHandler.sendMessageDelayed(mHandler.obtainMessage(1),2);

            }
            return n;
        }

        @Override
        protected void onPostExecute(Bitmap s) {
            super.onPostExecute(s);
            ZuanShiView zuanShiView = findViewById(R.id.zuanshi);
            zuanShiView.setColorList(list);
            zuanShiView.setH(s.getHeight());
            zuanShiView.setW(s.getWidth());
            zuanShiView.invalidate();
           // setData();

        }
    }
    int w =0;
    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){

                case 0:
                    createGrid(w);
                    break;
                case 1:
                    setData();
                    break;
            }
        }
    };


    private void createGrid(int w){

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,w);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(baseRecycleAdapter);


    }
    private void setData(){
        baseRecycleAdapter.setList(list);
        baseRecycleAdapter.notifyDataSetChanged();
    }
    public static Bitmap drawableToBitmap(Drawable drawable) {

        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();
        System.out.println("Drawable转Bitmap");
        Bitmap.Config config =
               /* drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                        : */Bitmap.Config.RGB_565;
        Bitmap bitmap = Bitmap.createBitmap(w, h, config);
        //注意，下面三行代码要用到，否则在View或者SurfaceView里的canvas.drawBitmap会看不到图
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h);
        drawable.draw(canvas);

        return bitmap;
    }
    /**
     * 按比例缩放图片
     *
     * @param origin 原图
     * @param ratio  比例
     * @return 新的bitmap
     */
    private Bitmap scaleBitmap(Bitmap origin, float ratio) {
        if (origin == null) {
            return null;
        }
        int width = origin.getWidth();
        int height = origin.getHeight();
        Matrix matrix = new Matrix();
        matrix.preScale(ratio, ratio);
        Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
        if (newBM.equals(origin)) {
            return newBM;
        }
        origin.recycle();
        return newBM;
    }

}
